package com.itheima.easy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.easy.base.AjaxResult;
import com.itheima.easy.base.PageResponse;
import com.itheima.easy.constant.AccraditationRecordConstant;
import com.itheima.easy.dto.ApplicationsDto;
import com.itheima.easy.entity.AccraditationRecord;
import com.itheima.easy.entity.CheckIn;
import com.itheima.easy.entity.User;
import com.itheima.easy.enums.Applications;
import com.itheima.easy.mapper.AccraditationRecordMapper;
import com.itheima.easy.mapper.ApplicationsMapper;
import com.itheima.easy.mapper.UserRoleMapper;
import com.itheima.easy.service.ApplicationsService;
import com.itheima.easy.utils.StringUtils;
import com.itheima.easy.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sjqn
 * @date 2023/6/28
 */
@Service
public class ApplicationsServiceImpl implements ApplicationsService {

    @Autowired
    private ApplicationsMapper applicationsMapper;

    private static final String RECORD_STEP_NO_PRE = "RECORD:";

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private AccraditationRecordMapper accraditationRecordMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 分页查询我的申请
     * @param applicationsDto
     * @return
     */
    @Override
    public AjaxResult<Applications> selectByPage(ApplicationsDto applicationsDto) {
        PageHelper.startPage(applicationsDto.getPageNum(),applicationsDto.getPageSize());
        Page<List<Applications>> page = applicationsMapper.selectByPage(applicationsDto);
        PageResponse<Applications> of = PageResponse.of(page, Applications.class);
        return AjaxResult.success(of);
    }

    @Override
    public void insert(Long userId, User currentUser, CheckIn checkIn, Integer status, String option) {
        //记录操作记录
        AccraditationRecord accraditationRecord = new AccraditationRecord();
        accraditationRecord.setApproverId(currentUser.getId());
        accraditationRecord.setBussniessId(checkIn.getId());
        accraditationRecord.setCreateTime(LocalDateTime.now());
        accraditationRecord.setType(AccraditationRecordConstant.RECORD_TYPE_CHECK_IN);
        accraditationRecord.setOpinion(option);
        accraditationRecord.setApproverName(currentUser.getRealName());
        accraditationRecord.setCurrentStep("发起申请-申请入住");
        accraditationRecord.setAuditStatus(status);
        accraditationRecord.setStepNo(redisTemplate.boundValueOps(RECORD_STEP_NO_PRE + checkIn.getId()).increment());

        //查询下一个审核人的姓名和角色
        List<UserRoleVo> userRoleVoList = userRoleMapper.selectByUserId(userId);
        //待办任务，下个节点审核人 设置下个操作人--->护理部主管
        UserRoleVo userRoleVo = userRoleVoList.get(0);

        //审核记录，设置下个节点审核人的角色和用户名
        accraditationRecord.setNextApprover(userRoleVo.getUserName());
        List<String> roleNameList = userRoleVoList.stream().map(x -> x.getRoleName()).collect(Collectors.toList());
        accraditationRecord.setNextApproverRole(StringUtils.join(roleNameList, ","));
        accraditationRecord.setNextApproverId(userRoleVo.getId());
        //保存审核（操作）记录
        accraditationRecordMapper.insert(accraditationRecord);
    }
}
